#!/bin/bash
# installux is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
# 
# Jon Peeters, July 2011

installux() {
	dir=$(basename "$PWD")
	if [[ ! -f "$HOME/.linappux_pgms" ]]; then
		zenity --info --text="${!_choose_linappux_dir_}"
		dirinstall=$(zenity --file-selection --directory --title="${!_linappux_dir_title_}")
		if [[ "$dirinstall" == "" ]]; then
			echo "No file to install programs..."
			exit
		fi
		if [[ -w "$dirinstall" ]]; then
			echo "$dirinstall" > "$HOME/.linappux_pgms"
			OUTDIR=$dirinstall/$dir
		else
			zenity --error "${!_permission_denied_}"
			exit
		fi
	else
		OUTDIR=$(cat $HOME/.linappux_pgms)/$dir
	fi
	if [[ ! -d "$OUTDIR" ]]; then
		mkdir "$OUTDIR"
		if [[ $? -ne 0 ]]; then
			zenity --error "${!_permission_denied_}"
			exit
		fi
	fi
	echo -en "Installing files in '${OUTDIR}'..."
	cp -rf * "$OUTDIR"
	if [[ $? -ne 0 ]]; then
		zenity --error --text="${!_not_successfully_installed_}"
		exit
	else
		zenity --info --text="${!_successfully_installed_}"
	fi
	rm "$OUTDIR/install.sh" 2>/dev/null
	if [[ -f "$OUTDIR/$dir.desktop" ]]; then
		desktop=""	
		if [[ -d "$HOME/Desktop" ]]; then
			desktop="$HOME/Desktop"
		elif [[ -d "$HOME/Bureau" ]]; then
			desktop="$HOME/Bureau"
		fi
		if [[ -n "$desktop" ]] && [[ ! -f "$desktop/$dir.desktop" ]]; then
			zenity --question --text="${!_create_shortcut_}"
			if [[ $? -eq 0 ]]; then
				sed "s|WORKING_DIRECTORY|$OUTDIR|" "$OUTDIR/$SEAdirectory.desktop" > "$desktop/$dir.desktop"
				chmod a+x "$desktop/$dir.desktop"
			fi
		fi
	fi
	echo -e "\nfinished.\n"
}

translate_labels() {
	language="${LANG:0:2}"
	if [[ -z "$language" ]] || ([[ "$language" != "en" ]] && [[ "$language" != "fr" ]]); then # No specific language
		language="en"
	fi
	_choose_linappux_dir_="_choose_linappux_dir_$language"
	_linappux_dir_title_="_linappux_dir_title_$language"
	_permission_denied_="_permission_denied_$language"
	_not_successfully_installed_="_not_successfully_installed_$language"
	_successfully_installed_="_successfully_installed_$language"
	_create_shortcut_="_create_shortcut_$language"
}

updatux_check() {
	if [[ ! -f "$OUTDIR/../updatux.sh" ]]; then
		wget "$repository/updatux.sh" -O "$OUTDIR/../updatux.sh" 2>/dev/null
		chmod a+x "$OUTDIR/../updatux.sh"
		if [[ $? -ne 0 ]]; then
			echo -e "\n!! Cannot install updatux... !!\n"
		fi
	fi
}

post_installux() {
	if [[ -f "$OUTDIR/post_installux.sh" ]]; then
		cd "$OUTDIR"
		./post_installux.sh
		if [ $? -eq 0 ]; then
			rm post_installux.sh
		fi
		cd -
	fi
}

_choose_linappux_dir_en="Choose the folder where programs will be installed"
_choose_linappux_dir_fr="Vous allez choisir le dossier ou\n seront installés les programmes"
_linappux_dir_title_en="Programs directory..."
_linappux_dir_title_fr="Dossier des programmes..."
_permission_denied_en="You do not have the necessary rights to install the program"
_permission_denied_fr="Vous n'avez pas les droits nécessaires\npour installer ce programme"
_not_successfully_installed_en="The program <b>$dir</b> has not been properly installed"
_not_successfully_installed_fr="Le programme <b>$dir</b> n'a pas été correctement installé"
_successfully_installed_en="The program <b>$dir</b> has been properly installed"
_successfully_installed_fr="Le programme <b>$dir</b> a été correctement installé"
_create_shortcut_en="Create a shortcut on the desktop?"
_create_shortcut_fr="Créer un raccourci sur le bureau?"
repository="http://linappux.googlecode.com/hg"

translate_labels
installux
updatux_check
post_installux
